import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/role_permission_entity.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/pop/normal_pop.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';

class UserNameDownPopInputField extends StatefulWidget {
  final FocusNode focusNode;
  final TextEditingController controller;
  final bool hasError;
  final RolePermissionEntity defaultRole;
  final ValueChanged roleCallback;
  final double horizontalPadding;
  final List<RolePermissionEntity> roles;

  const UserNameDownPopInputField(
      {Key key,
      this.horizontalPadding,
      this.focusNode,
      this.hasError,
      this.roleCallback,
      this.defaultRole,
      this.controller,
      this.roles = const []})
      : super(key: key);

  @override
  _UserNameDownPopInputFieldState createState() =>
      _UserNameDownPopInputFieldState();
}

class _UserNameDownPopInputFieldState extends State<UserNameDownPopInputField> {
  bool _hasFocus = false;
  RolePermissionEntity currentSelectRole;

  @override
  void initState() {
    super.initState();
    currentSelectRole = widget.defaultRole;
    widget.focusNode?.addListener(() {
      if (widget.focusNode.hasFocus) {
        if (_hasFocus == false) {
          if (mounted)
            setState(() {
              _hasFocus = true;
            });
        }
      } else {
        if (_hasFocus == true) {
          if (mounted)
            setState(() {
              _hasFocus = false;
            });
        }
      }
    });
  }

  void onSelectPersonType(RolePermissionEntity value) {
    FocusScope.of(context).requestFocus(FocusNode());
    setState(() {
      currentSelectRole = value;
    });
    widget.roleCallback?.call(currentSelectRole);
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: widget.horizontalPadding ?? getDp(45),
      ),
      child: Row(
        children: [
          Expanded(
            child: Container(
                height: getDp(54),
                padding: EdgeInsets.symmetric(
                  horizontal: getDp(12),
                ),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: _hasFocus == true
                      ? Border.all(
                          color: aceBlueColor,
                          width: getDp(1),
                        )
                      : Border(),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.25),
                      blurRadius: getDp(22),
                      spreadRadius: -getDp(5),
                    ),
                  ],
                  borderRadius: BorderRadius.circular(getDp(15)),
                ),
                child: Row(
                  children: [
                    SvgPicture.asset(
                      A.assetsImagesStyleUser_1,
                      width: getDp(15),
                      height: getDp(15),
                    ),
                    SizedBox(
                      width: getDp(15),
                    ),
                    Expanded(
                        child: TextField(
                      inputFormatters: [
                        LengthLimitingTextInputFormatter(40),
                      ],
                      controller: widget.controller,
                      focusNode: widget.focusNode,
                      scrollPadding: EdgeInsets.zero,
                      obscureText: false,
                      keyboardType: TextInputType.text,
                      style: TextStyle(
                        fontSize: getSp(16),
                        color: aceBlackColor,
                        fontWeight: FontWeight.bold,
                      ),
                      decoration: InputDecoration(
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          disabledBorder: InputBorder.none,
                          hintText: S.current.member_name,
                          hintStyle: TextStyle(
                            color: widget.hasError == true
                                ? aceRedColor
                                : aceGrayColor,
                            fontWeight: FontWeight.w700,
                            fontSize: getSp(16),
                          )),
                    )),
                  ],
                )),
          ),
          Row(
            children: [
              SizedBox(
                width: getDp(8),
              ),
              SizedBox(
                  width: getDp(125),
                  height: getDp(54),
                  child: NormalPop<RolePermissionEntity>(
                    horizontalPadding: 0,
                    select: widget.defaultRole,
                    group: widget.roles,
                    buildDisplayName: (e) => e.name,
                    hint: '',
                    onSelect: (data) => onSelectPersonType(data),
                  )),
            ],
          )
        ],
      ),
    );
  }
}
